<?php

class SmspushController extends Controller_Base
{

    /**
     * 创蓝
     * @throws Exception
     */
    public function CLStautsAction()
    {

        $request = $this->getRequest();
        $msgid = $request->getQuery('msgid', '');
        $reportTime = $request->getQuery('reportTime', 0);
        $mobile = $request->getQuery('mobile', '');
        $status = $request->getQuery('status', '');
        $notifyTime = $request->getQuery('notifyTime', 0);
        $statusDesc = $request->getQuery('statusDesc', '');
        $length = $request->getQuery('length', 0);
        $log = "push: ".json_encode($_REQUEST);
        Func_Log::write('SMS_Notify', "chuanglanNotify", $log);
        if (!empty($mobile) || !empty($status) || !empty($msgid) || !empty($statusDesc) || !empty($notifyTime) || !empty($reportTime) || !empty($length)) {

            $sms_record = Module_Mediate_SmsRecord::getInstance()->getRecordByMobileTaskid($mobile,$msgid,'Chuanglan');
            if(empty($sms_record)){
                Func_Log::write('SMS_Notify', "chuanglanNotify", $log . ' sms_log not fount');
                $return = ["clcode" => "111111"];
                echo json_encode($return);
                exit();
            }
            if ($status == "DELIVRD") {
                $res = Module_Mediate_SmsRecord::getInstance()->singleUpdate([
                    'id'=>$sms_record['id'],
                    'sendStatus'=>1,
                    'sendResult'=>$statusDesc,
                ]);
                Func_Log::write('SMS_Notify', "chuanglanNotify", $log . ' $res=' . $res);
            } else {
                $res = Module_Mediate_SmsRecord::getInstance()->singleUpdate([
                    'id'=>$sms_record['id'],
                    'sendStatus'=>4,
                    'sendResult'=>$statusDesc,
                ]);
                Func_Log::write('SMS_Notify', "chuanglanNotify", $log . ' $res=' . $res);
            }
            if (!$res) {
                $return = ["clcode" => "111111"];
                echo json_encode($return);
                exit();
            }
            $return = ["clcode" => "000000"];
            echo json_encode($return);
            exit();
        }
        $return = ["clcode" => "000000"];
        echo json_encode($return);
        exit();
    }

    /**
     * 创蓝
     * @throws Exception
     */
    public function CLReplyAction()
    {
        $request = $this->getRequest();
        $moTime = $request->getQuery('moTime', 0);
        $mobile = $request->getQuery('mobile', '');
        $msg = $request->getQuery('msg', '');
        $receive_time = strtotime('20'.$moTime);
        $log = "push: ".json_encode($_REQUEST);
        Func_Log::write('SMS_Notify', "chuanglanReply", $log );
        if(!empty($mobile) && !empty($msg)){
            $sms_log = Module_Mediate_SmsRecord::getInstance()->getRecordByMobile($mobile);
            $mobile_send = [
                'mobile' => $mobile,
                'content' => $msg,
                'replyTime' => $receive_time ? $receive_time : time(),
                'smsRecordContent' => $sms_log['content'],
                'caseId' => $sms_log['caseId'],
                'mediatorId' => $sms_log['mediatorId'],
                'mediator' => $sms_log['mediator'],
                'mediationOrganId' => $sms_log['mediationOrganId'],
            ];
            $receive = Module_Mediate_SmsReplyRecord::getInstance()->singleInsert($mobile_send);
            Func_Log::write('SMS_Notify', "chuanglanReply", "add success " . $log );
            if(!$receive){
                $return = ["clcode" => "111111"];
                echo json_encode($return);
                exit();
            }

            $return = ["clcode" => "000000"];
            echo json_encode($return);
            exit();
        }

        $return = ["clcode" => "111111"];
        echo json_encode($return);
        exit();

    }
    /**
     * 联麓
     * @throws Exception
     */
    public function lianluStautsAction()
    {
        $raw_result = file_get_contents("php://input");//这样也可以获取
        $result = json_decode($raw_result, 1);
        $msgid = empty($result['taskId']) ? '' : $result['taskId'];
        $mobile = empty($result['phone']) ? '' : $result['phone'];
        $statusDesc = empty($result['codeDesc']) ? '' : $result['codeDesc'];
        $status = empty($result['respCode']) ? '' : $result['respCode'];
        $log = "push: ".$raw_result;
        Func_Log::write('SMS_Notify', "lianluStatus", $log);

        if (!empty($mobile) || !empty($status) || !empty($msgid) || !empty($statusDesc)) {
            $sms_record = Module_Mediate_SmsRecord::getInstance()->getRecordByMobileTaskid($mobile,$msgid,'LianLu');
            if(empty($sms_record)){
                Func_Log::write('SMS_Notify', "lianluStatus", $log . ' sms_log not fount');
                $return = ["clcode" => "111111"];
                echo json_encode($return);
                exit();
            }
            if(empty($sms_record)){
                Func_Log::write('SMS_Notify', "lianluStatus", $log . ' sms_log not fount');
                $return = ["clcode" => "111111"];
                echo json_encode($return);
                exit();
            }
            if ($status == "DELIVRD") {
                $res = Module_Mediate_SmsRecord::getInstance()->singleUpdate([
                    'id'=>$sms_record['id'],
                    'sendStatus'=>1,
                    'sendResult'=>$statusDesc,
                ]);
                Func_Log::write('SMS_Notify', "lianluStatus", $log . ' $res=' . $res);
            } else {
                $res = Module_Mediate_SmsRecord::getInstance()->singleUpdate([
                    'id'=>$sms_record['id'],
                    'sendStatus'=>4,
                    'sendResult'=>$statusDesc,
                ]);
                Func_Log::write('SMS_Notify', "lianluStatus", $log . ' $res=' . $res);
            }
            if (!$res) {
                $return = ["clcode" => "111111"];
                echo json_encode($return);
                exit();
            }
            $return = ["clcode" => "000000"];
            echo json_encode($return);
            exit();
        }
        $return = ["clcode" => "000000"];
        echo json_encode($return);
        exit();
    }

    /**
     * 联麓
     * @throws Exception
     */
    public function lianluReplyAction(){
        $raw_result = file_get_contents("php://input");//这样也可以获取
        $result = json_decode($raw_result, 1);
        $taskId = empty($result['taskId']) ? '' : $result['taskId'];
        $mobile = empty($result['phone']) ? '' : $result['phone'];
        $msg = empty($result['contentUp']) ? '' : $result['contentUp'];
        $timestamp = empty($result['timestamp']) ? '' : $result['timestamp'];
        $timestam = $timestamp ? intval($timestamp / 1000) : 0 ;
        $log = "push: ".$raw_result;
        Func_Log::write('SMS_Notify', "lianluReply", $log);
        if(!empty($mobile) && !empty($msg)){
            $sms_log = Module_Mediate_SmsRecord::getInstance()->getRecordByMobile($mobile);
            $mobile_send = [
                'mobile' => $mobile,
                'content' => $msg,
                'replyTime' =>  $timestam ? $timestam : time(),
                'smsRecordContent' => $sms_log['content'],
                'caseId' => $sms_log['caseId'],
                'mediatorId' => $sms_log['mediatorId'],
                'mediator' => $sms_log['mediator'],
                'mediationOrganId' => $sms_log['mediationOrganId'],
            ];
            $receive = Module_Mediate_SmsReplyRecord::getInstance()->singleInsert($mobile_send);
            Func_Log::write('SMS_Notify', "lianluReply", "add success " . $log );
            if(!$receive){
                $return = ["clcode" => "111111"];
                echo json_encode($return);
                exit();
            }

            $return = ["clcode" => "000000"];
            echo json_encode($return);
            exit();
        }

        $return = ["clcode" => "111111"];
        echo json_encode($return);
        exit();
    }



}